package com.yrh.controller;

import com.yrh.bean.TUser;
import com.yrh.service.TUserService;
import com.yrh.service.impl.TUserServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
//用户控制层
@WebServlet("/UserServlet/*")
@SuppressWarnings("all")
public class UserServlet extends BaseServlet {
    //创建用户逻辑层对象
    private TUserService service=new TUserServiceImpl();
    //用户登录
    public void login(HttpServletRequest request, HttpServletResponse response) throws Exception {
     //获取前台用户的参数使用map接收
        Map<String, String[]> map = request.getParameterMap();
        // 获取的参数对象化
        TUser tUser=new TUser();
        //使用工具类把map数据封装为javabean
        BeanUtils.populate(tUser,map);
        TUser user = service.login(tUser);
        if(user!=null){
            if ("Y".equals(user.getUserStatus())){
                request.getSession().setAttribute("user",user);
                //响应信息
                response.getWriter().println(1);
            }else {
                //用户账号信息停用
                response.getWriter().println(-1);
            }
        }else {
            //用户账号密码错误
            response.getWriter().println(0);
        }
    }
    //用户退出
    public void out(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //清除用户保存session中的数据
        request.getSession().removeAttribute("user");
        //重定向到首页
        response.sendRedirect(request.getContextPath()+"/main.jsp");
    }
    //用户注册
    public void register(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取页面信息
        Map<String, String[]> map = request.getParameterMap();
        TUser u=new TUser();
        //封装数据
        BeanUtils.populate(u,map);
        //用户名是否被占用
        if (service.checkTel(u)){
            //注册用户
            int i = service.addTUser(u);
            if (i==1){ //如果注册成功自动登陆
                TUser user = service.login(u);
                request.getSession().setAttribute("user", user);
                response.getWriter().print(1);
            }else {
                response.getWriter().print(0);
            }
        }else {//被占用
            response.getWriter().print(-1);
        }
    }
    //修改用户信息
    public void update(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取前台的参数封装为JavaBean
        Map<String, String[]> map = request.getParameterMap();
        TUser u=new TUser();
        BeanUtils.populate(u,map);
        //获取用户信息
        TUser user = (TUser) request.getSession().getAttribute("user");
        u.setUserId(user.getUserId());
        //获取前台验证码
        String code = request.getParameter("code");
        //获取浏览器保存的验证码
        String code1 = (String) request.getSession().getAttribute("code");
        if (!"".equals(code)&&code!=null&&!code1.equalsIgnoreCase(code)){
            response.getWriter().println(-1);
            return;
        }
        //修改用户信息
        if (service.updateTUser(u)==1){//修改成功
            if ("".equals(u.getUserPwd())){ //修改用户重置session
                //重新设置session信息
                user.setUserName(u.getUserName());
                user.setUserSex(u.getUserSex());
                request.getSession().setAttribute("user",user);
            }else {//修改密码清空session重新登录
                request.getSession().removeAttribute("user");
            }
            response.getWriter().println(1);
        }else {//修改失败
            response.getWriter().println(0);
        }

    }
    //分页查询所有用户
    public void allTUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取前台的参数封装为JavaBean
        Map<String, String[]> map = request.getParameterMap();
        TUser u=new TUser();
        BeanUtils.populate(u,map);
        //查询所有用户信息去前台渲染
        List<TUser> tUsers = service.allTUser(u,0,0);
        //分页操作
        //分页操作
        int size=2;//每页的数据量
        int totalPage=tUsers.size()%size==0?tUsers.size()/size:tUsers.size()/size+1;//计算出多少页
        int nowPage=1;//默认显示第一页;
        if (request.getParameter("nowPage")!=null){
            nowPage=Integer.parseInt(request.getParameter("nowPage"));
        }
        tUsers=service.allTUser(u,nowPage,size);
        request.setAttribute("uList",tUsers);
        request.setAttribute("nowPage",nowPage);
        request.setAttribute("totalPage",totalPage);
        request.setAttribute("beginTime",u.getBeginTime());
        request.setAttribute("endTime",u.getEndTime());
        request.setAttribute("userTel",u.getUserTel());
        request.getRequestDispatcher("/user_list.jsp").forward(request,response);
    }
    //用户禁用或启用
    public void Status(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取前台数据
        String userStatus = request.getParameter("userStatus");
        String userId = request.getParameter("userId");
        String nowPage = request.getParameter("nowPageS");
        TUser u=new TUser();
        u.setUserStatus(userStatus);
        u.setUserId(Integer.parseInt(userId));
        int i = service.updateTUser(u);
        request.setAttribute("nowPage",nowPage);
        if (i>0){
            response.getWriter().println(1);
        }else {
            response.getWriter().println(-1);
        }
    }
}
